package com.eco.fanliapp.job.orderjobNew;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.eco.fanliapp.common.OrderTaskUtils;
import com.eco.fanliapp.entity.*;
import com.eco.fanliapp.service.*;
import com.eco.fanliapp.utils.BigDecimalUtils;
import com.eco.fanliapp.utils.DateUtils;
import org.apache.commons.collections.CollectionUtils;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * 根据订单详情，补充订单
 */
@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class OrderCheckTask implements Job, Serializable {

	private static final Logger log = LoggerFactory.getLogger(OrderCheckTask.class);

	@Autowired
	private IAppOrderDetailService appOrderDetailService;

	@Autowired
	private IAppUserService appUserService;

	@Autowired
	private IAppUserOrderService appUserOrderService;

	@Autowired
	private IUserCashschemeService userCashschemeService;

	@Autowired
	private IJobOrderInfoHandleNewService jobOrderInfoHandleNewService;

	@Autowired
	private  ITaoBaoOrderNewService taoBaoOrderNewService;

	@Autowired
	private OrderTaskUtils orderTaskUtils;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {
		//取得job详情
		JobDetail jobDetail = context.getJobDetail();
		JobDataMap dataMap =  jobDetail.getJobDataMap();

		int executeCount = 0;
		if (dataMap.containsKey("runCount")) {
			executeCount = dataMap.getInt("runCount");
		}
		// 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
		executeCount++;
		dataMap.put("runCount", executeCount);

		long beginTime = System.currentTimeMillis();
		log.info("***************************************开始执行拉检查订单是否遗漏定时任务-OrderCheckTask：executeCount=" + executeCount + "--beginTime=" + beginTime);

		//获取淘宝联盟账号-排除已过期的
		List<TaobaoScheme> taobaoSchemes = orderTaskUtils.getUsedTaobaoSchemeList();

//		//根据商品详情，查询哪些订单丢失
//		List<AppOrderDetail> list = appOrderDetailService.orderCheckList();
//		if(CollectionUtils.isNotEmpty(list)){
//			for(AppOrderDetail aod:list){
//				if (aod.getTableCreateTime() >= (System.currentTimeMillis() - 1000L * 60 * 40)) {
//					continue;
//				}
//				//创建一条付款订单信息（包括上级）
//				createAppUserOrder(aod, taobaoSchemes);
//			}
//		}
		List<AppOrderDetail> list = appOrderDetailService.orderCheckUserOrderList();
		if(CollectionUtils.isNotEmpty(list)){
			for(AppOrderDetail aod:list){
				if ("3".equals(aod.getPayStatus())) {
					String startTime = DateUtils.long2DatestrByFormt(aod.getEarningTime(), null);
					String endTime = DateUtils.long2DatestrByFormt((aod.getEarningTime() + 1000L * 60 * 3), null);
					log.info("检查订单是否遗漏定时任务结算订单startTime{}endTime{}", startTime, endTime);
					//jobOrderInfoHandleNewService.handleSettleOrder(startTime, endTime, taobaoSchemes, 1);
					log.info("检查订单是否遗漏定时任务结束结算订单startTime{}endTime{}", startTime, endTime);
					try {
						taoBaoOrderNewService.handlDisSettleOrderList(aod);
					} catch (Exception e) {
						e.printStackTrace();
					}
				}


			}

		}

		long endTime = System.currentTimeMillis();
		log.info("***************************************结束执行定时任务-检查订单是否遗漏定时任务-OrderCheckTask：executeCount=" + executeCount + "--endTime=" + endTime + "--耗时=" + (endTime - beginTime));

	}


}
